# keywords.py
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from loguru import logger

"""
element_to_be_clickable:直到元素可被点击
presence_of_element_located:直到元素出现
invisibility_of_element_located:直到元素可见
visibility_of_element_located : 元素是否可见
location_once_scrolled_into_view : 方法把某个元素滚动到可视范围内。这个方法并不够灵活. 
"""


DEFAULT_TIMEOUT = 30
from utils.basic import get_path

class KeyWords:

    def __init__(self, driver: WebDriver):
        self.driver = driver

    def get(self, url):
        """页面访问"""
        self.driver.get(url)

    # 元素是否可点击
    def is_enable_click(self, locator, timeout=None) -> WebElement:
        timeout = DEFAULT_TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)
        return wait.until(expected_conditions.element_to_be_clickable(locator))

    # 元素是否可见
    def is_visible(self, locator, timeout=None) -> WebElement:
        timeout = DEFAULT_TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)
        return wait.until(expected_conditions.visibility_of_element_located(locator))

    # 元素是否可见 : 主要判断find_elements中的情况
    def is_visibles(self, locator, timeout=None) -> WebElement:
        timeout = DEFAULT_TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)
        return wait.until(expected_conditions.visibility_of_all_elements_located(locator))

    def sleep(self,time):
        sleep(time)

    # 移动鼠标
    def move_to_element(self,locator):
        el = self.is_enable_click(locator)
        ActionChains(self.driver).move_to_element(el).perform()

    # 元素点击
    def click(self, locator):
        """页面点击"""
        el = self.is_enable_click(locator)
        # el.location_once_scrolled_into_view()
        el.click()

    # 元素输入
    def send_keys(self, locator, words):
        """页面输入"""
        el = self.is_visible(locator)
        el.send_keys(words)

    # 获取页面文本
    def text(self,locator,index=None):
        if index is not None:
            el = self.is_visibles(locator)
            if isinstance(el,list):
                text = el[index].text
                logger.info("获取页面文本为:{}".format(text))
                return text
            else:
                logger.error("返回的类型不正确，必须为数组，当前为:{}".format(el))

        el = self.is_visible(locator)
        return el.text

    # 切换浏览器句柄
    def switch_to(self,index=-1):
        self.driver.switch_to.window(self.driver.window_handles[index])

    # 截图
    def screen(self,png_name):
        png_file = get_path('screen',png_name)
        self.driver.get_screenshot_as_file(png_file)



if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get("http://localhost")
    keyword = KeyWords(driver)
    keyword.click(('link text','登录'))